public with sharing class ClsCreateQuoteFromOpportunity {

    //For Properties...
    public Quote quoteObject{get;set;}
    public string opportunityId {get;set;}
    public string accountName {get;set;}
    public Opportunity oppObject {get;set;}
    //For Constructor...
    public ClsCreateQuoteFromOpportunity(ApexPages.StandardController controller) 
    {
        //For Quote Object...
        //quoteObject = new Quote(Name='Temp');
        quoteObject = new Quote();
        oppObject = null;
        
        if(ApexPages.CurrentPage().getParameters().get('oppid') != null)
        {
            opportunityId = ApexPages.CurrentPage().getParameters().get('oppid');
            quoteObject.OpportunityId = ApexPages.CurrentPage().getParameters().get('oppid');
            
            System.debug('The Opportunity Id from URL => '+ opportunityId);
            
            //For Opportunity Pricebook and Account Name...
            oppObject = [Select Pricebook2Id, Name, Id, CurrencyIsoCode, Account.Name, AccountId, (Select Id,Product_Group__c,Product_Group_Description__c,Product_Group_Master__c,Inquiry_Part_No__c,Delivery_Days__c,UOM__c, OpportunityId, SortOrder, PricebookEntryId, CurrencyIsoCode, Quantity, TotalPrice, UnitPrice, ListPrice, ServiceDate, Description, CreatedDate, CreatedById, LastModifiedDate, LastModifiedById, SystemModstamp, IsDeleted, Part_Number__c, VAT_Value__c, VAT_Code__c, VAT_Rate__c, Commission__c, Freight_Rate__c,Duty__c, Margin__c, EFACS_Quote_ID__c,Item_Number__c, Gross_Amount__c,Discount From OpportunityLineItems ORDER BY Item_Number__c,SortOrder,CreatedDate ASC) From Opportunity where id =: opportunityId];
            
            if(oppObject != null)
            {
                accountName = oppObject.Account.Name;
                quoteObject.Pricebook2Id = oppObject.Pricebook2Id;
                //Field is not writeble...
                //quoteObject.CurrencyIsoCode = oppObject.CurrencyIsoCode; 
            }
        }
        
        
    }
    
    
    //For Save Quote Method...
    public PageReference saveQuoteMethod()
    {
        Savepoint sp = Database.setSavepoint();
        
        try
        {
        	System.debug('The Opportunity Id =>'+ quoteObject.OpportunityId );
        	System.debug('The Account Id =>'+ oppObject.AccountId );
            //Inserting the Quote...
            insert quoteObject; 
            
            List<QuoteLineItem> quoteLineItemList = new List<QuoteLineItem>();
            integer counter = 0;
            //Now Creating the Quote Line Items...
            for(OpportunityLineItem oppItemObject : oppObject.OpportunityLineItems)
            {
                QuoteLineItem quoteItemObject = new QuoteLineItem();
                quoteItemObject.QuoteId =  quoteObject.Id;
                System.debug('The Opportunity Line Item Id => '+ oppItemObject.Id);
              
                //For Standard Field Mapping...
                quoteItemObject.PricebookEntryId = oppItemObject.PricebookEntryId;
                quoteItemObject.ServiceDate = oppItemObject.ServiceDate;
                quoteItemObject.Description = oppItemObject.Description;
                quoteItemObject.Discount = oppItemObject.Discount;
                quoteItemObject.Quantity = oppItemObject.Quantity;
                quoteItemObject.UnitPrice = oppItemObject.UnitPrice;
                //quoteItemObject.CurrencyIsoCode = oppItemObject.CurrencyIsoCode;
                //quoteItemObject.ListPrice = oppItemObject.ListPrice;
                //quoteItemObject.Subtotal = oppItemObject.Subtotal;
                                
                //For Custom Field Mapping ....
                quoteItemObject.Commission__c = oppItemObject.Commission__c;
                quoteItemObject.Duty__c = oppItemObject.Duty__c;
                quoteItemObject.VAT_Code__c = oppItemObject.VAT_Code__c;
                quoteItemObject.Margin__c = oppItemObject.Margin__c;
                quoteItemObject.VAT_Rate__c = oppItemObject.VAT_Rate__c;
                quoteItemObject.VAT_Value__c = oppItemObject.VAT_Value__c;
                quoteItemObject.Freight_Rate__c = oppItemObject.Freight_Rate__c;
                quoteItemObject.Part_Number__c = oppItemObject.Part_Number__c;
                //quoteItemObject.Gross_Amount__c = oppItemObject.Gross_Amount__c;
                quoteItemObject.Item_Number__c = counter + 1;
                quoteItemObject.Opportunity_Line_Item_Id__c = oppItemObject.Id;
                quoteItemObject.EFACS_Quote_ID__c = oppItemObject.EFACS_Quote_ID__c;
                quoteItemObject.UOM__c = oppItemObject.UOM__c;
                quoteItemObject.Delivery_Days__c = oppItemObject.Delivery_Days__c;
                quoteItemObject.Inquiry_Part_No__c = oppItemObject.Inquiry_Part_No__c; 
                quoteItemObject.Product_Group_Master__c = oppItemObject.Product_Group_Master__c;
                quoteItemObject.Product_Group_Description__c = oppItemObject.Product_Group_Description__c;
                quoteItemObject.Product_Group__c = oppItemObject.Product_Group__c;
                //Adding in the List...
                quoteLineItemList.add(quoteItemObject); 
                
                //Adding in the Counter ...
                counter ++;
            }
            /*
            for(QuoteLineItem tempString : quoteLineItemList)
            {
            	System.debug('The Quote Opportunity Line Item Id => '+ tempString.Opportunity_Line_Item_Id__c);
            }
            */
            
            if(quoteLineItemList.size() > 0)
                insert quoteLineItemList;
                
            //return the values...
            PageReference pageRef = new PageReference('/'+ quoteObject.id);
            return pageRef;
            
        }
        catch(Exception exp)
        {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'The Error => '+ exp.getLineNumber()+' '+exp.getMessage()); ApexPages.addMessage(msg); Database.rollback(sp);
            
            //If we get the error... just clone the same Object to keep the values...   
            quoteObject = quoteObject.clone(false); 
            
            //Returning the values...
            return null;    
                
        }
        
    }
    
    public PageReference pullingContactFieldMethod()
    {
        System.debug('Inside the Pulling fuunction.... ');
        if(quoteObject.ContactId != null)
        {
            Contact contactObject = null;
            contactObject = [select Email,Fax,Phone FROM Contact where Id =: quoteObject.ContactId];
            
            if(contactObject != null)
            {
                
                quoteObject.Email = contactObject.Email;
                quoteObject.Fax = contactObject.Fax;
                quoteObject.Phone = contactObject.Phone;
                //quoteObject.ShippingCountry = contactObject.Email;
                
                System.debug('The Quote Email => ' + quoteObject.Email );
            }
        }
        return null;
    }
    //For Cancellation Methods...
    public PageReference cancelQuoteMethod()
    {
        if(opportunityId != null)
        {
            PageReference pageRef = new PageReference('/'+ opportunityId);
            return pageRef;
        }
        else
        {
            Pagereference pageRef = new PageReference('/home/home.jsp');
            return pageRef;
        }
        
    }
    
    /*public PageReference SaveQuote()
    {
      
      Opportunity  oppObj = new Opportunity();
      ID oppId ;
      
      
      if(ApexPages.CurrentPage().getParameters().get('oppid') != null){
            oppId= ApexPages.CurrentPage().getParameters().get('oppId');
       
      oppObj =[select  Chase_Date__c , Customer_Code__c , Customer_Reference__c , Initial_Interest__c , Interstart_Delivery_Code__c , 
                      Opportunity_Credit_Period_In_Days__c , Opportunity_Credit_Limit__c , Opportunity_Settlement_Terms__c , 
                      Quotation_Date_Time__c , Revision__c ,
                       AccountId , Name , Description from Opportunity where id = : oppId]; 
         
      Account accObj = new Account();
      accObj = [select name , ShippingStreet, ShippingState, ShippingPostalCode, ShippingCountry, ShippingCity, BillingStreet, BillingState, BillingPostalCode, BillingCountry,BillingCity from Account where id  = :oppObj.AccountId] ;   
      
      
      
      
            
      Quote quote = new Quote();
      quote.opportunityId = oppObj.id ;
      quote.BillingName = accObj.Name ;
      quote.ShippingName = accObj.Name ;
      quote.Description = oppObj.Description ;
      quote.ShippingStreet = accObj.ShippingStreet ; 
      quote.ShippingState = accObj.ShippingState ;
      quote.ShippingPostalCode = accObj.ShippingPostalCode ; 
      quote.ShippingCountry = accObj.ShippingCountry ; 
      quote.ShippingCity = accObj.ShippingCity ; 
      quote.BillingStreet = accObj.BillingStreet ; 
      quote.BillingState = accObj.BillingState ; 
      quote.BillingPostalCode = accObj.BillingPostalCode ; 
      quote.BillingCountry = accObj.BillingCountry ;
      quote.BillingCity = accObj.BillingCity ;
      
      
      quote.Chase_Date__c = oppObj.Chase_Date__c ;
      quote.Customer_Code__c = oppObj.Customer_Code__c ;
      quote.Customer_Reference__c = oppObj.Customer_Reference__c ;
      quote.Initial_Interest__c = oppObj.Initial_Interest__c ;
      quote.Interstart_Delivery_Code__c = oppObj.Interstart_Delivery_Code__c ; 
      quote.Opportunity_Credit_Limit__c = oppObj.Opportunity_Credit_Limit__c ;
      quote.Opportunity_Credit_Period_In_Days__c = oppObj.Opportunity_Credit_Period_In_Days__c ;
      quote.Opportunity_Settlement_Terms__c = oppObj.Opportunity_Settlement_Terms__c ;
      quote.Quotation_Date_Time__c = oppObj.Quotation_Date_Time__c ;
      quote.Revision__c = oppObj.Revision__c ;
      
      quote.Name = 'Temp Quote' ;
      insert quote ;
      
      
      PageReference pageRef = new PageReference('/'+ quote.id);
      return pageRef ;
      
            
      }      
      
         
      
    
    
    
    
    
      
     return null ;
    }*/
    
    
    
    
    
    
    

}